package com.material.vtool.controller;

import com.material.vtool.entity.BTeamMaterialMessageEntity;
import com.material.vtool.entity.reqEntity.MaterialCommentReqBean;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.service.TeamMaterialMessageService;
import com.material.vtool.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 团队成员素材的留言日志
 */
@RestController
@RequestMapping("/message")
public class TeamMaterialMessageController {
    @Autowired
    private TeamMaterialMessageService teamMaterialMessageService;

    /**
     * 素材留言
     * @param userId 用户ID
     * @param comment 评论
     * @return
     */
    @PostMapping(value = "/materialComment", produces = "application/json;charset=UTF-8")
    public RespData materialComment(@RequestHeader("userId") Integer userId, @RequestBody MaterialCommentReqBean comment) {
        if (comment == null || comment.getTeamMaterialId().intValue() <= 0 || StringUtils.isBlank(comment.getMessage())) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        BTeamMaterialMessageEntity bTeamMaterialMessageEntity = new BTeamMaterialMessageEntity();
        bTeamMaterialMessageEntity.setTeamMaterialId(comment.getTeamMaterialId());
        bTeamMaterialMessageEntity.setUserId(userId);
        bTeamMaterialMessageEntity.setMessage(comment.getMessage());
        bTeamMaterialMessageEntity.setCreateTime(DateUtil.getCurDate());
        teamMaterialMessageService.save(bTeamMaterialMessageEntity);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
}
